package com.car.order.aop;


import com.car.commons.web.util.StateUtil;
import com.car.order.annotation.StockLogAnnotation;
import com.car.order.dto.goodOrderInfo.GoodOrderInfoDto;
import com.car.order.dto.goodOrderInfo.PayOrderForm;
import com.car.order.entity.Stock;
import com.car.order.entity.StockLog;
import com.car.order.entity.form.stock.StockUpdateForm;
import com.car.order.mapper.GoodOrderLogMapper;
import com.car.order.mapper.StockLogMapper;
import com.car.order.util.GoodsOrderDescConstants;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
@Aspect
@Slf4j
public class StockLogAop {
    @Resource
    private StockLogMapper stockLogMapper;
    @Around("@annotation(com.car.order.annotation.StockLogAnnotation)")
    public Object log(ProceedingJoinPoint pjp) throws Throwable {
        //获取注解
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        StockLogAnnotation declaredAnnotation = signature.getMethod().getDeclaredAnnotation(StockLogAnnotation.class);
        /* 获取方法入参 */
        Object[] args = pjp.getArgs();
        // 获取注解上的，订单操作描述
        System.out.println("<>?");
        Integer desc = declaredAnnotation.desc();
        StockLog stockLog = new StockLog();
        Object proceed = pjp.proceed();
        stockLog.setChangeType(desc);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        stockLog.setModifyTime(LocalDateTime.now().format(formatter));
        Stock proceed1 = (Stock) proceed;
        stockLog.setAdminId(proceed1.getAdminId());
        stockLog.setStockId(proceed1.getId());
        for (Object arg : args) {
            if (arg instanceof StockUpdateForm) {
                StockUpdateForm form = (StockUpdateForm) arg;
                stockLog.setChangeQuantity(form.getStock());
            }
        }
        stockLogMapper.insert(stockLog);
        return proceed;
    }


}
